//implementing list using arrays
//praneet sharma
//2010061
#include<stdio.h>
#include<math.h>
#include<string.h>


//declaration of the functions
int append(int a[],int x,int len)
{
	a[len]=x;
	len=len+1;
	return a,len;
}


int extend_list(int a[],int b[],int len1,int len2)
{
	int i;
	for(i=0;i<=len2-1;i++)
	{
		a,len1=append(a,b[i],len1);
	}
	return a,len1;
}
		

	

int index_list(int a[],int x,int len)
{
	int i,flag=0,l=0;
	for(i=0;i<=len-1;i++)
	{
		if(x==a[i])
		{
			printf("the value is found\n");
			printf("the position of the value is %d",i);
			printf("\n");
			break;
			flag=0;	
		}
		else if(x!=a[i])
		{
			flag=1;
		}
		l=l+flag;
	}
	if(l==len)
	{
		printf("value is not in the list\n");
	}
}




int insert_val(int a[],int x,int pos,int len)
{
	int b[100],i,j;
	for(i=0;i<len;i++)
	{
		b[i]=a[i];
	}
	a[pos]=x;
	len++;
	for(j=pos+1;j<=len;j++)
	{
		a[j]=b[j-1];
	}
	return a,len;
}

int remove_val(int a[],int x,int len)
{
	int b[100],i,j,k,pos;
	for(i=0;i<len;i++)
	{
		b[i]=a[i];
	}
	
	for(k=0;k<=len-1;k++)
	{
		if(a[k]==x)
		{
			pos=k;
		}
	}
	for(j=pos;j<=len;j++)
	{
		a[j]=b[j+1];
	}
	len--;
	return a,len;
}	
	

int pop_val(int a[],int x,int len)
{
	int b[100],i,j,k,pos;
	for(i=0;i<len;i++)
	{
		b[i]=a[i];
	}
	
	for(k=0;k<=len-1;k++)
	{
		if(a[k]==a[x])
		{
			pos=k;
		}
	}
	for(j=pos;j<=len;j++)
	{
		a[j]=b[j+1];
	}
	len--;
	return a,len;
}

int count_el(int a[],int x,int len)
{
	int count=0,i;
	for(i=0;i<=len-1;i++)
	{
		if(a[i]==x)
		{
			count=count+1;
		}
	}
	printf("the number of times %d has occured is %d",x,count);
	printf("\n");
}


int insertion_sort(int a[],int len)
{
	int i,j,key;
	for(j=1;j<=len-1;j++)
	{
		i=j-1;
		key=a[j];
		while(i>0 && a[i]>key)
		{
			a[i+1]=a[i];
			i=i-1;
		}
	a[i+1]=key;
	}
	return a,len;
}

int reverse_list(int a[],int len)
{
		int i,k;
		if(len%2==0)
		{	
			for(i=0;i<=(len/2)-1;i++)
			{
			
				a[i]=a[i]+a[len-1-i];
				a[len-1-i]=a[i]-a[len-1-i];
				a[i]=a[i]-a[len-1-i];
			}
		}
		else if(len%2!=0)
		{
			for(i=0;i<=((len-1)/2)-1;i++)
			{
			
				a[i]=a[i]+a[len-1-i];
				a[len-1-i]=a[i]-a[len-1-i];
				a[i]=a[i]-a[len-1-i];
			}
		}
		return a,len;
}	
		
		

int main()
{
	int choice,i,a[50],x,len,j,ser,val_ins,pos_ins,in,swa_el,sw,pos_el,pw,cou,re,ins,n1,ex,a1[50],exl;
	char ch;

	printf("enter the length of the array:");
	scanf("%d",&len);
	for(i=0;i<=len-1;i++)
	{
		printf("enter the value at %dth position:",i);
		scanf("%d",&a[i]);
	}
	do{	
		printf("-------------\n");
		printf("function list\n");
		printf("--------------\n");
		printf("1 to append\n");
		printf("2 to extend\n");
		printf("3 to insert\n");
		printf("4 to remove\n");
		printf("5 to pop\n");
		printf("6 to index\n");
		printf("7 to count\n");
		printf("8 to sort\n");
		printf("9 to reverse\n");
		printf("10 to exit\n");
		printf("enter your choice :");
		scanf("%d",&choice);

		switch(choice){
				
				case 1:{
					printf("enter the value you want to append : ");
					scanf("%d",&x);
					a,len=append(a,x,len);
					printf("Array after appending the value is:");
					for(j=0;j<=len-1;j++)		
					{
						printf("%d ",a[j]);
					}
					printf("\n");
					
					break;
					}

		

				case 2:{
					printf("enter the number of elements in the sublist you want to extend:");
					scanf("%d",&n1);
					for(ex=0;ex<=n1-1;ex++)
					{
						printf("enter the value at %dth position of the sublist:",ex);
						scanf("%d",&a1[ex]);
					}
					a,len=extend_list(a,a1,len,n1);
					printf("the list after extinding is:");
					for(exl=0;exl<=len-1;exl++)
					{
						printf("%d ",a[exl]);
					}
					break;
				      }	
				
				case 3:{
					printf("enter the value you want to insert:");
					scanf("%d",&val_ins);
					printf("\n");
					printf("enter the position where you want to insert the value:");
					scanf("%d",&pos_ins);
					printf("\n");
				
					a,len=insert_val(a,val_ins,pos_ins,len);
					printf("value after inserting in an array is :");
					for(in=0;in<=len-1;in++)
					{
						printf("%d ",a[in]);
					}
					break;
				       }
			
				case 4:{
					printf("enter the element you want to remove:");
					scanf("%d",&swa_el); 
					a,len=remove_val(a,swa_el,len);
					printf("the array after removing the element is:");
					for(sw=0;sw<=len-1;sw++)
					{
						printf("%d ",a[sw]);	
					}
					printf("\n");
					break;
				       }
				case 5:{
					printf("enter the position of the element you want to remove:");
					scanf("%d",&pos_el); 
					a,len=pop_val(a,pos_el,len);
					printf("the array after removing the element is:");
					for(pw=0;pw<=len-1;pw++)
					{
						printf("%d ",a[pw]);	
					}
					printf("\n");
					break;
				       }
				case 6:{
					printf("enter the element you want to search:");
					scanf("%d",&ser);
					index_list(a,ser,len);
					break;
				       }
				case 7:{
					printf("enter the element whose occurences you want to count:");
					scanf("%d",&cou);
					count_el(a,cou,len);
					break;
				       }
				case 8:{
					a,len=insertion_sort(a,len);
					printf("the list in sorted order is:");
					for(ins=0;ins<=len-1;ins++)
					{
						printf("%d ",a[ins]);
					}
					printf("\n");
					break;
				       }
					
				case 9:{
					a,len=reverse_list(a,len);
					printf("the list in reverse order is:");
					for(re=0;re<=len-1;re++)
					{
						printf("%d ",a[re]);
					}
					printf("\n");
					break;
				       }


				case 10:return;
					


						
								
				}
		printf("\n");
		setbuf(stdin,NULL);
		printf("do you want to continue(y,n):");
		ch=getchar();
		printf("\n");
	
	}while(ch=='y'|ch=='Y');	
}
			
	
	
